package com.yuanjing.xiangqinxiangai.plugin.auth

import android.content.Context
import android.util.Log
import com.blankj.utilcode.util.ResourceUtils
import org.json.JSONException
import org.json.JSONObject
import java.io.IOException

class ConsoleConfigManager private constructor(ctx: Context) {
    var config: ConsoleConfig? = null

    init {
        try {
            config = ConsoleConfig()
            val json = ResourceUtils.readAssets2String(FILE_NAME)
            val jsonObject = JSONObject(json)
            config?.parseFromJSONObject(jsonObject)
        } catch (e: IOException) {
            Log.e(javaClass.name, "初始配置读取失败", e)
            config = null
        } catch (e: JSONException) {
            Log.e(javaClass.name, "初始配置读取失败, JSON格式不正确", e)
            config = null
        } catch (e: Exception) {
            Log.e(javaClass.name, "初始配置读取失败, JSON格式不正确", e)
            config = null
        }
    }

    companion object {
        private const val FILE_NAME = "console_config.json"

        private var instance: ConsoleConfigManager? = null

        fun getInstance(ctx: Context): ConsoleConfigManager {
            if (instance == null) {
                synchronized(ConsoleConfigManager::class.java) {
                    if (instance == null) {
                        instance = ConsoleConfigManager(ctx.applicationContext)
                    }
                }
            }
            return instance!!
        }
    }
}
